package com.youlai.boot.modules.merchant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.merchant.model.MerchantForm;
import com.youlai.boot.modules.merchant.model.MerchantVO;
import com.youlai.boot.modules.merchant.model.MerchantPageQuery;
import com.youlai.boot.common.result.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import com.youlai.boot.modules.merchant.service.MerchantService;
import com.youlai.boot.modules.brand.service.BrandService;
import com.youlai.boot.modules.brand.model.BrandVO;
import com.youlai.boot.modules.brand.model.BrandPageQuery;

@Tag(name = "18, 商户管理", description = "商户管理")
@RestController
@RequestMapping("/api/v1/merchant")
@RequiredArgsConstructor
public class MerchantController {

    private final MerchantService merchantService;
    private final BrandService brandService;

    @Operation(summary = "商户列表")
    @GetMapping("/page")
    public PageResult<MerchantVO> getMerchantPage(MerchantPageQuery query) {
        IPage<MerchantVO> page = merchantService.getMerchantPage(query);
        return PageResult.success(page);
    }

    @Operation(summary = "获取表单")
    @GetMapping("/{id}/form")
    public Result<MerchantForm> getForm(@PathVariable Long id) {
        MerchantForm form = merchantService.getForm(id);
        return Result.success(form);
    }

    @Operation(summary = "保存")
    @PostMapping
    public Result<Boolean> save(@RequestBody MerchantForm form) {
        boolean result = merchantService.save(form);
        return Result.judge(result);
    }

    @Operation(summary = "更新")
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody MerchantForm form) {
        boolean result = merchantService.update(id, form);
        return Result.judge(result);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = merchantService.removeById(id);
        return Result.judge(result);
    }

    // @Operation(summary = "商户品牌列表，关联查询")
    // @GetMapping("/{id}/brands")
    // public PageResult<BrandVO> getBrands(@PathVariable Long id, @Valid
    // BrandPageQuery query) {
    // IPage<BrandVO> page = brandService.getBrandsByMerchantId(id, query);
    // return PageResult.success(page);
    // }
}
